package com.smartens.boit.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.smartens.boit.MyApplication2;
import com.smartens.boit.R;
import com.smartens.boit.activity.viewmodel.ControlViewMode;
import com.smartens.boit.adapter.MyTableViewAdapter;
import com.smartens.boit.base.BaseActivity;
import com.smartens.boit.bean.RecordDetail;
import com.smartens.boit.bean.RecordeDetail_datd;
import com.smartens.boit.fragment.AutoControlFragment;
import com.smartens.boit.fragment.ManualControlFragment;
import com.smartens.boit.util.EditTextUtil;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 设备控制
 * @Author songtt
 * @Date 2021/3/4
 */

public class ControlActivity extends BaseActivity {
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private MyTableViewAdapter tableViewAdapter;
    private LinearLayout back;
    private EditText liteDataEdit;
    private boolean isfirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        //找控件
        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mViewPager = (ViewPager) findViewById(R.id.fragment_vp);
        back = findViewById(R.id.tv_back);
        liteDataEdit = findViewById(R.id.showLiteEdit);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ControlActivity.this.finish();
            }
        });

        //初始化视图
        initViews();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    private void initViews() {
        String[] mTitles = new String[]{"手动控制", "自动控制"};
        //使用适配器将ViewPager与Fragment绑定在一起
        tableViewAdapter = new MyTableViewAdapter(getSupportFragmentManager(), mTitles);
        mViewPager.setAdapter(tableViewAdapter);
        //将TabLayout与ViewPager绑定在一起
        mTabLayout.setupWithViewPager(mViewPager);
        //指定Tab的位置
        one = mTabLayout.getTabAt(0);
        two = mTabLayout.getTabAt(1);
        HttpHelper.getInstance().getDeviceOnOffDetailResult(new HttpHelper.HttpCallBack<RecordDetail>() {
            @Override
            public void onSuccess(List<RecordDetail> result) {
                if (result != null && result.size() > 0) {
                    int i = result.get(result.size() - 1).getSwitchMode();
                    Log.v("dfsdfsdfsdsdf","-----------"+i);
                    if (i == 0) {
                        two.select();
                    } else {
                        one.select();
                    }
                    Log.v("aaSelectedListener", "select");
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (isfirst) {
                    isfirst = false;
                    return;
                }

                try {
                    int i;
                    if (tab.getPosition() == 0) {
                        i = 1;
                    } else {
                        i = 0;
                    }
                    Log.v("aaSelectedListener", "onTabSelected");
                    HttpHelper.getInstance().getUpdateOpenModeGataWayResult(i, new HttpHelper.HttpCallBack() {
                        @Override
                        public void onSuccess(List result) {
                            Log.v("aOpenModeGataWayResult", "成功");
                        }

                        @Override
                        public void onFailure(String message) {
                            Log.v("aOpenModeGataWayResult", "失败");
                        }
                    });
                } catch (Exception e) {
                    Log.v("aOpenModeGataWayResult", e.getMessage());

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (isfirst) {
                    isfirst = false;
                    return;
                }
                Log.v("aaSelectedListener", "onTabSelected2");
            }
        });
      //  initEditTextUtil();
    }
    private  void initEditTextUtil()
    {
            View view=getWindow().getDecorView();
        EditTextUtil editTextUtil=EditTextUtil.getInstance(view,this);

    }

    @Override
    public void onResume() {
        super.onResume();
        MyApplication2 myApplication = (MyApplication2) getApplication();
        myApplication.isControl = true;

        Log.v("activity.isControl", "true");
    }

    @Override
    public void onStop() {
        super.onStop();
        MyApplication2 myApplication = (MyApplication2) getApplication();
        myApplication.isControl = false;
        Log.v("activity.isControl", "false");
    }

    public void setclose() {
        ManualControlFragment manualControlFragment = (ManualControlFragment) tableViewAdapter.getItem(2);
        //     manualControlFragment.CheckBoxAllClose();
    }
}